package com.kaifamiao.service.imp;

import com.kaifamiao.mapper.DoctorMapper;
import com.kaifamiao.mapper.IDepartmentMapper;
import com.kaifamiao.pojo.Department;
import com.kaifamiao.pojo.Doctor;
import com.kaifamiao.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DepartmentService implements IDepartmentService {
    @Autowired
    IDepartmentMapper mapper;
    @Autowired
    DoctorMapper doctorMapper;
    @Override
    public List<Department> findAll() {
        List<Department> list = mapper.findAll();
//      System.out.println(list);
        return list;
    }

    @Override
    public void delete(int id) {
            mapper.delete(id);
    }

    @Override
    public Department selectById(int id) {
        return mapper.selectById(id);
    }

    @Override
    public void update(Department department) {
        mapper.update(department);
    }

    @Override
    @Transactional
    public void add(Department department,List<Doctor> doctors) {
        mapper.add(department);
        for (Doctor doctor:doctors) {
            doctor.setEpartmene_id(department.getDepartid());
            doctorMapper.addDoctor(doctor);
        }
    }

    @Override
    public List<Department> selectDoctorAndDepart(int id) {
        return mapper.selectDoctorAndDepart(id);
    }
}
